package com.zhongxia.controller;

import com.zhongxia.entity.Admin;
import com.zhongxia.entity.User;
import com.zhongxia.feign.AccountFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.ReflectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;

/**
 * @Author: 废柴弟弟
 * @Date: 2020/4/9 16:44
 */

@Controller
@RequestMapping("/clientaccount")
public class ClientAccount {

    @Autowired
    private AccountFeign accountFeign;

    @PostMapping("/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("type") String type, HttpSession session){
        Object account = accountFeign.login(username,password,type);
        LinkedHashMap<String,Object> hashMap=(LinkedHashMap) account;
        String target = null;
        String nickname="";
        int id=0;
        if(account == null){
            target = "login";
        }else{
            switch (type){
                case "user":
                    User user = new User();
                    id=(int)hashMap.get("id");
                    nickname=(String)hashMap.get("nickname");
                    user.setId(id);
                    user.setNickname(nickname);
                    session.setAttribute("user",user);
                    target = "index";
                    break;
                case "admin":
                    Admin admin = new Admin();
                    id=(int)hashMap.get("id");
                    String username2=(String)hashMap.get("username");
                    admin.setId(id);
                    admin.setUsername(username2);
                    session.setAttribute("admin",admin);
                    target = "main";
                    break;
            }
        }
        return target;
    }

    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }

    @RequestMapping("/redirect/{target}")
    public String redirect(@PathVariable("target") String target){
        return target;
    }

}
